package com.sttg.csp.registration.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.sttg.csp.common.action.PagedAction;
import com.sttg.csp.common.consts.SystemConst;
import com.sttg.csp.common.exception.BizException;
import com.sttg.csp.registration.service.BDUserRegService;

public class BDUserRegAction extends PagedAction {

	private BDUserRegService bDUserRegService = BDUserRegService.getInstance();

	private Map model = new HashMap();

	private File file;// 对应的java文件对象

	private String fileFileName;// 对应的java文件名称

	private String fileContentType;// 对应的java文件类型

	public BDUserRegAction() {
	}

	// show Index page
	public String showIndex() throws Exception {
		return INDEX;
	}

	// query list of B/D administrators.
	public String queryList() throws Exception {
		try {
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			String sessionUserName = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_USER_NAME");
			String sessionDepartment = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_DEPARTMENT");
			model.put("SESSION_UID", sessionUid);
			model.put("SESSION_USER_NAME", sessionUserName);
			model.put("SESSION_DEPARMENT", sessionDepartment);
			rows = bDUserRegService.queryList(model);
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		return LIST;
	}

	// show detail of a B/D certificate association record 
	public String showInfo() throws Exception {
		try {
			model = bDUserRegService.queryObject(model);
			if (model == null) {
				this.addActionError("The record no found in ldap!");
				return ERROR_PAGE;
			}
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		setOperationType(SystemConst.OPERATION_TYPE_VIEW);
		return INFO;
	}

	// show page for choose resource of certificate
	public String showChooseResourceOfCer() throws Exception {
		return "choose_resource_of_cer";
	}
	
	// show page of certificate list
	public String showCerList() throws Exception {
		
		String sourceOfCerList = (String) model.get("SOURCE_OF_CER_LIST");
		
		if (SystemConst.KEY_FILE.equals(sourceOfCerList)) {
			logger.debug("Upload file path is: " + file.getAbsolutePath());
			logger.debug("Upload file name is: " + fileFileName);
			logger.debug("Upload file content type is: " + fileContentType);
			
			//TODO Here is that reading certificate list from key file.
		} else {
			//TODO Here is that reading certificate list from token.
		}
		
		for (int i = 0; i < 5; i++) {
			Map<String, String> each = new HashMap<String, String>();
			each.put("CER_NAME", "Certificate " + i);
			rows.add(each);
		}
		return "cer_list";
	}

	// show page of certificate list
	public String doConfirmPassword() throws Exception {
		try {
			//TODO Assume that password of certificate is correct.
			
			String sessionUid = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_UID");
			String sessionUserName = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_USER_NAME");
			String sessionDepartment = (String) ((Map)session.get(SystemConst.SESSION_USER)).get("SESSION_DEPARTMENT");
			model.put("SESSION_UID", sessionUid);
			model.put("SESSION_USER_NAME", sessionUserName);
			model.put("SESSION_DEPARTMENT", sessionDepartment);
			
			if (SystemConst.OPERATION_TYPE_SELF_REGISTRATION == operationType) {
				bDUserRegService.doInsert(model);
				
			} else if (SystemConst.OPERATION_TYPE_CONFIRM_REGISTRATION == operationType) {
				bDUserRegService.doUpdate(model);
			}
		} catch (BizException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
		
		return NONE;
	}
	
	public void setModel(Map model) {
		this.model = model;
	}

	public Map getModel() {
		return model;
	}

	
	public File getFile() {
		return file;
	}

	
	public void setFile(File file) {
		this.file = file;
	}

	
	public String getFileFileName() {
		return fileFileName;
	}

	
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	
	public String getFileContentType() {
		return fileContentType;
	}

	
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
}
